package com.nsapi.niceschoolapi.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nsapi.niceschoolapi.common.base.PageData;
import com.nsapi.niceschoolapi.common.config.MySysUser;
import com.nsapi.niceschoolapi.entity.*;
import com.nsapi.niceschoolapi.service.EvaluateService;
import com.nsapi.niceschoolapi.service.StudentService;
import com.nsapi.niceschoolapi.service.TchCourseService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.naming.ldap.PagedResultsControl;
import java.util.List;
import java.util.Map;

@Controller
public class ExamController {

    @Resource
    private EvaluateService evaluateService;

    @Resource
    private StudentService studentService;

    @Resource
    private TchCourseService tchCourseService;


    //进入查询页面
    @RequestMapping(value = "/selectGrade")
    public String selectGrade(){
        return "view/student/selGrade";
    }

    //查询学生成绩
    @RequestMapping(value = "/selGrade")
    @ResponseBody
    public PageData<StuExamVO> selGrade(){
        StudentDB studentDB=studentService.student(MySysUser.loginName());
        PageData<StuExamVO> pageData=new PageData<>();
        List<StuExamVO> stuExamList=evaluateService.stuExamList(studentDB.getSid());
        pageData.setData(stuExamList);
        return pageData;
    }
    //进入成绩录入页面
    @RequestMapping(value = "/selstudentExam")
    public String studentExam(){
        return "StudentExam";
    }
    //查询学生选课
    @RequestMapping(value = "/selScs")
    @ResponseBody
    public PageData<StudentVO> selScs(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                      @RequestParam(value = "limit" , defaultValue = "10") Integer limit,
                                        StudentVO studentVO){
        TeacherDB teacherDB=tchCourseService.teacher(MySysUser.loginName());
        studentVO.setTid(teacherDB.getTid());
        PageData<StudentVO> pageData=new PageData<>();
        PageHelper.startPage(page,limit);
        List<StudentVO> studentVOList=evaluateService.studentList(studentVO);
        PageInfo pageInfo=new PageInfo(studentVOList);
        pageData.setData(pageInfo.getList());
        pageData.setCount(pageInfo.getTotal());
        return pageData;
    }
    @ResponseBody
    @RequestMapping("selmessage")
    public Object selmessage(StuExamVO stuExamVO){
        String msg="";
        int i = evaluateService.selExam(stuExamVO);
        if(i==1){
            msg="请不要重复导入成绩";
        }else{
            int num = evaluateService.add(stuExamVO);
            if (num==1){
                msg="添加成功";
            }
            else {
                msg="添加失败";
            }
        }
        return msg;
    }
    //进入学生成绩页面
    @RequestMapping(value = "/tchSelExam")
    public String tchSelExam(){
        return "view/student/tchSelStuExam";
    }
    @RequestMapping("/selAllExam")
    @ResponseBody
    public LayuiResult<Map> selAllExam(SelAllExamVO selAllExamVO, Integer page, Integer limit){
        PageHelper.startPage(page, limit);
        List<Map> exam = evaluateService.selAllExam(selAllExamVO);
        PageInfo pageInfo = new PageInfo(exam);
        LayuiResult<Map> result = new LayuiResult<>();
        //这是layui要求返回的json数据格式
        result.setCode(0);
        result.setMsg( "");
        //将全部数据的条数作为count传给前台（一共多少条）
        result.setCount(pageInfo.getTotal());
        //将分页后的数据返回（每页要显示的数据）
        result.setData(pageInfo.getList());
        return result;
    }
    @ResponseBody
    @RequestMapping("saveupdate")
    public  Object saveupdate(StuExamDB stuExamDB){
        String msg="";
        int nums = evaluateService.updateExam(stuExamDB);
        if(nums==1){
            msg="修改成功";
        }
        return msg;
    }
}
